package com.example.zp.greenfruit;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.zp.fragment.Classify_fragment2;
import com.example.zp.fragment.Home_fragment1;
import com.example.zp.fragment.My_fragment4;
import com.example.zp.fragment.Shopping_fragment3;
import com.example.zp.fragment.Topfragment;
import com.example.zp.util.Contant;

public class MainActivity extends AppCompatActivity {
RelativeLayout r_lay;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        r_lay =findViewById(R.id.r_lay);

        initTop();//顶部布局
        initFoot();//底部导航栏

    }

    private void initFoot() {
        FragmentTabHost tabHost = findViewById(R.id.host);
        tabHost.setup(this,getSupportFragmentManager(),R.id.flay);

        for (int i=0; i<4 ;i++){
            TabHost.TabSpec tabSpec = tabHost.newTabSpec(i+"");
            View view = getLayoutInflater().inflate(R.layout.foot_item,null);

            ImageView fot_img = view.findViewById(R.id.fot_img);
            fot_img.setImageResource(Contant.SPEC_IMG[i]);

            TextView fot_text = view.findViewById(R.id.fot_text);
            fot_text.setText(Contant.SPEC_TEXT[i]);

            tabSpec.setIndicator(view);//添加视图

            switch (i){
                case 0:
                    tabHost.addTab(tabSpec,Home_fragment1.class,null);
                    break;
                case 1:
                    tabHost.addTab(tabSpec,Classify_fragment2.class,null);
                    break;
                case 2:
                    tabHost.addTab(tabSpec,Shopping_fragment3.class,null);
                    break;
                case 3:
                    tabHost.addTab(tabSpec,My_fragment4.class,null);
                    break;
                default:
                    break;
            }
        }
    }

    private void initTop() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction top_ft = manager.beginTransaction();
        top_ft.add(R.id.top_lay,new Topfragment());
        top_ft.addToBackStack(null);
        top_ft.commit();
    }
}
